<template>
    <div class="app-container">
        <h4 class="form-header h4">基本信息</h4>
        <el-form ref="form" label-width="80px">
            <el-row>
                <el-col :span="8" :offset="2">
                    <el-form-item label="用户名称" prop="userName">
                        <div class="inputStyle">
                            {{ this.$route.query.userName }}
                        </div>
                    </el-form-item>
                </el-col>
                <el-col :span="8" :offset="2">
                    <el-form-item label="登录账号" prop="updateUser">
                        <div class="inputStyle">
                            {{ this.$route.query.loginName }}
                        </div>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <h4 class="form-header h4">角色信息</h4>
        <el-table :row-key="getRowKey" @row-click="clickRow" ref="table" @selection-change="handleSelectionChange"
            :data="userList">
            <el-table-column label="序号" type="index" align="center">
                <template slot-scope="scope">
                    <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
                </template>
            </el-table-column>
            <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
            <el-table-column label="角色编号" align="center" prop="roleId" />
            <el-table-column label="角色名称" align="center" prop="roleName" />
            <el-table-column label="权限字符" align="center" prop="roleKey" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template slot-scope="scope">
                    <span>{{(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryInfo.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>

        <el-form label-width="100px">
            <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
                <el-button type="primary" @click="submitForm()">提交</el-button>
                <el-button @click="close()">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>

export default {
    name: "SelectUser.vue",
    data() {
        return {
            total: 0,
            defaultProps: {
                children: "children",
                label: "menuName"
            },
            // 遮罩层
            loading: true,
            menuOptions: [],
            userList: [],
            // 分页信息
            total: 0,
            pageNum: 1,
            pageSize: 10,
            // 选中角色编号
            roleIds: [],
            // 角色信息
            roles: [],
            // 用户信息
            form: {},
            queryInfo: {
                // 当前的页数
                currentPage: 1,
                // 当前每页显示多少条数据
                pageSize: 10,
                userId: undefined,
            },
            updateUser: undefined,
        };
    },
    created() {
        this.queryInfo.userId = this.$route.query.userId
        this.getList()
        // const userId = this.$route.query.userId
        // const userId = this.$route.params && this.$route.params.userId;
    },
    methods: {
        // 渲染列表
        async getList() {
            const userId = this.$route.query.userId
            const updateUser = this.$route.query.updateUser
            console.log(updateUser);
            if (userId) {
                const p = {
                    userId: this.queryInfo.userId,
                    pageSize: this.queryInfo.pageSize,
                    currentPage: this.queryInfo.currentPage
                }
                const { data: res } = await this.$http.post('/sys-user/allocationRolePage', p)
                if (res.code === 20000) {
                    this.form = res.data.sysRolePage.records
                    this.userList = res.data.sysRolePage.records
                    console.log(res.data.sysRolePage.records);
                    this.total = res.data.sysRolePage.records.length
                }
            } else {
                this.$message.error(res.msg)
            }
        },

        /** 单击选中行数据 */
        clickRow(row) {
            this.$refs.table.toggleRowSelection(row);
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.roleIds = selection.map((item) => item.roleId);
        },
        // 保存选中的数据编号
        getRowKey(row) {
            return row.roleId;
        },
        /** 提交按钮 */
        async submitForm() {
            const userId = this.$route.query.userId
            const roleIds = this.roleIds.join(",");
            if (userId && roleIds) {
                const { data: res } = await this.$http.post('/sys-user/allocationRole', { userId, roleIds:this.roleIds })
                if (res.code === 20000) {
                    this.getList()
                    this.$message.success(`授权成功!`)
                    this.close()
                } else {
                    this.$message.success(res.message)
                }
            } else {
                this.$message.warning(`请选择要授权的用户!`)
            }
            console.log(roleIds);
        },
        /** 关闭按钮 */
        close() {
            // const obj = { path: "/system/user" };
            // this.$tab.closeOpenPage(obj);
            this.$router.push('/users')
        },
        // 监听 pagesize 改变的事件
        handleSizeChange(newSize) {
            console.log(newSize)
            this.queryInfo.pageSize = newSize
            this.getList()
        },
        // 监听页码值改变的事件
        handleCurrentChange(newPage) {
            console.log(newPage)
            this.queryInfo.currentPage = newPage
            this.getList()
        },
    },
};
</script>

<style scoped lang="less">
.inputStyle {
    width: 200px;
    height: 36px;
    border: 1px solid grey;
    justify-content: center;
    line-height: 36px;
    padding-left: 20px;
    border-radius: 10px;
}

.app-container {
    background-color: #ffffff;
    height: 95%;
    padding: 20px;
}
</style>